﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Top.Api;

namespace hnrzxt.API
{
    [Route("api/[controller]")]
    [ApiController]
    public class UploadFileController : ControllerBase
    {
        private readonly IWebHostEnvironment _webHostEnvironment;

        public UploadFileController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }

        [HttpPost("/upload")]
        public async Task<IActionResult> UploadFileAsync(List<IFormFile> file)
        {
            long size = file.Sum(f => f.Length);
            List<string> fileNames = new List<string>();
            //Dictionary<string, string> temp = new Dictionary<string, string>();

            foreach (var file1 in file)
            {
                var filename = Path.GetFileName(file1.FileName);
                //拼接完整的文件存储路径
                var filePath = Path.Combine("wwwroot\\UploadFiles", filename);
                //temp.Add("src",filename);
                fileNames.Add(filename);
                using (var fs = System.IO.File.OpenWrite(filePath))
                {
                    await file1.CopyToAsync(fs);
                }
            } 
            
            return Ok(new { code = 0, Msg = "ok", date = fileNames });
        }
    }
}